package cn.wawi.utils;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.github.pagehelper.PageHelper;

/**
 * @description 查询工具类
 * @author 龚亮
 * @date 2014-10-18 12:21:34
 */
public class QueryUtil{

	private static final Integer PAGE_SIZE=10;         //默认每页显示10条数据
	private static final Integer PAGE_INDEX=1;         //默认当前页为第一页
    private static String fileUploadPath=null;
    private static String fileDownPath=null;
	static {
		try {
			fileUploadPath = ResourceBundle.getBundle("jdbc").getString("fileUploadPath");
			fileDownPath = ResourceBundle.getBundle("jdbc").getString("fileDownPath");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过request构造查询Map模型
	 * @param request 注意数组只能取第一个
	 * @return
	 */
	public static Map<String,Object> setPage(HttpServletRequest request){
		Map<String,Object> map=getparamaterMap(request);
		PageHelper.startPage(toPageIndex(map),toPageSize(map));     //设置分页
		return map;
	}
	/**
	 * 通过request构造查询Map模型,自定义分页
	 * @param request 注意数组只能取第一个
	 * @param pageIndex  当前页
	 * @param pageSize   每页显示多少条数据
	 * @return
	 */
	public static Map<String,Object> setPage(HttpServletRequest request,int pageIndex,int pageSize){
		PageHelper.startPage(pageIndex,pageSize);     //设置分页
		return getparamaterMap(request);
	}
	/**
	 * 通过request构造查询Map模型,不分页
	 * @param request 注意数组只能取第一个
	 * @return
	 */
	public static Map<String,Object> setNoPage(HttpServletRequest request){
		return getparamaterMap(request);
	}
	/**
	 * 获取paramater参数，过滤空值
     */
	@SuppressWarnings("all")
	public static Map<String,Object> getparamaterMap(HttpServletRequest request){
		Map<String,Object> map=new HashMap<String,Object>();
		Enumeration names=request.getParameterNames();
		while(names.hasMoreElements()){
		   String name=(String) names.nextElement();
		   String value=request.getParameter(name);
		   if(null!=value&&!"".equals(value.trim())){//过滤空值或空字符串
			   map.put(name, value.trim());
		   }
		}
		return map;
	}
	/**
	 * 字符串转pageIndex
	 */
	public static Integer toPageIndex(Map<String,Object> map) {
		String pageIndex=(String) (map.containsKey("page")?map.get("page"):map.get("pageIndex"));
		if (null == pageIndex || "".equals(pageIndex.trim())) {
			return PAGE_INDEX;
		} else {
			return Integer.parseInt(pageIndex);
		}
	}
	/**
	 * 字符串转pageSize
	 */
	public static Integer toPageSize(Map<String,Object> map) {
		String pageSize=(String) (map.containsKey("rows")?map.get("rows"):map.get("pageSize"));
		if (null == pageSize || "".equals(pageSize.trim())) {
			return PAGE_SIZE;
		} else {
			return Integer.parseInt(pageSize);
		}
	}
	/*
	 * 设置文件上传
	 */
    public static Map<String,Object> fileUpload(HttpServletRequest request){
    	Map<String,Object> map=new HashMap<String,Object>();
    	CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
    	 if(multipartResolver.isMultipart(request)){ 
    		 map.put("fileDownPath", fileDownPath);
    		 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);
    		 map=getparamaterMap(multiRequest);
             Iterator<String> iter = multiRequest.getFileNames();
             int i=0;
             while(iter.hasNext()){  
                 MultipartFile file = multiRequest.getFile(iter.next());  
                 if(file != null){  
                	 i++;
                	 String myFileName = file.getOriginalFilename();  
                     if(myFileName.trim() !=""){  
                         String fileName=StringUtil.generateRandomDateDir()+StringUtil.generateRandonFileName(file.getOriginalFilename());
                    	 map.put("file"+i, fileName);
                         String path = fileUploadPath + fileName;  
                         File localFile = new File(path);  
                         try {
                        	if(!localFile.exists()&&!localFile.isDirectory())
                        		localFile.mkdirs();
							file.transferTo(new File(path));
						} catch (IllegalStateException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}  
                     }  
                 }
             }
    	 }else{
    		 map=getparamaterMap(request);
    	 }
    	 return map;
    }
}
